//
//  VerifyDuplicates.swift
//  RsyncUI
//
//  Created by Thomas Evensen on 24/10/2024.
//

import Foundation
import OSLog

@MainActor
struct VerifyDuplicates {
    private var arrayofhiddenIDs = [Int]()

    private func checkForDuplicates() throws {
        let uniqueIDs = Set(arrayofhiddenIDs)
        guard arrayofhiddenIDs.count == uniqueIDs.count else {
            throw DuplicateError.duplicate
        }
    }

    @discardableResult
    init(_ configurations: [SynchronizeConfiguration]) {
        for configuration in configurations {
            arrayofhiddenIDs.append(configuration.hiddenID)
        }
        do {
            try checkForDuplicates()
        } catch let err {
            let error = err
            propagateError(error: error)
        }
    }

    func propagateError(error: Error) {
        SharedReference.shared.errorobject?.alert(error: error)
    }
}

enum DuplicateError: LocalizedError {
    case duplicate

    var errorDescription: String? {
        switch self {
        case .duplicate:
            "Oh my, you've got a duplicate hiddenID!"
        }
    }
}
